<template>
  <div class="login-container">
    <div class="box">
      <div class="avanter">
        <img src="../assets/iu.jpg" alt />
      </div>
      <!-- 用户名及密码 -->
      <el-form :model="Login" label-width="0px" class="login_form" :rules="loginrules" ref="LoglinRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="Login.username"></el-input>
        </el-form-item>
        <!-- Mima -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-message-solid" v-model="Login.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <div class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      Login: {
        username: "admin",
        password: "123456"
      },
      loginrules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    reset(){
      console.log(this)
      this.$refs.LoglinRef.resetFields();
    },
    login(){
      this.$refs.LoglinRef.validate(async valid=>{
        // console.log(valid)
        if(!valid) return;
       const {data:res}=await this.$http.post('login',this.Login)
      //  console.log(res)
      if (res.meta.status!==200){
        this.$message({
          message: '用户名或密码输入错误',
          type: 'error'
        });
      }else{
          this.$message({
          message: '登陆成功',
          type: 'success',
          
        });
          console.log(res);
          window.sessionStorage.setItem('token',res.data.token)
          this.$router.push('/home')
      }
      })
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  background: #2b4b6b;
  height: 100%;
}
.box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avanter {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .login_form {
    position: absolute;
    bottom: 20px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>